package com.redisson.utils;

import org.redisson.api.RLock;

import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * Redisson分布式锁接口
 * <p>
 * RLock的实现有可重入非公平锁（RedissonLock）、
 * 可重入公平锁（RedissonFairLock）、
 * 联锁（RedissonMultiLock）、
 * 红锁（RedissonRedLock）、
 * 读锁（RedissonReadLock）、
 * 写锁（RedissonWriteLock）
 * @author zqf
 */
public interface DistributedLocker {
    RLock lock(String lockKey);

    RLock lock(String lockKey, long timeout);

    RLock lock(String lockKey, TimeUnit unit, long timeout);

    boolean tryLock(String lockKey, TimeUnit unit, long leaseTime);

    boolean tryLock(String lockKey, TimeUnit unit, long waitTime, long leaseTime);

    void unlock(String lockKey);

    void unlock(RLock lock);


    /**
     * @param lockKey
     * @param unit
     * @param waitTime
     * @param leaseTime
     * @param supplier 获取锁后要执行的业务逻辑
     * @param scene 业务逻辑的场景，用于打印日志
     * @param <T>
     * @return
     */
    <T> T tryLockAndRun(String lockKey, TimeUnit unit, long waitTime, long leaseTime, Supplier<T> supplier, String scene);

}
